package com.navi.heartbeat.job.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.lang.reflect.Method;

/**
 * Created by Devin on 2017/7/2.
 */
public class InvokingJobDetailDetailFactory extends QuartzJobBean {

    // 计划任务所在类
    private String heartBeatJob;

    // 具体需要执行的计划任务
    private String checkHeartBeat;

    private ApplicationContext ctx;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            Object otargetObject = ctx.getBean(heartBeatJob);
            Method m = null;
            Method m2 = null;
            try {
                m = otargetObject.getClass().getMethod(checkHeartBeat);
                m.invoke(otargetObject);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            throw new JobExecutionException(e);
        }
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.ctx = applicationContext;
    }

    public String getHeartBeatJob() {
        return heartBeatJob;
    }

    public void setHeartBeatJob(String heartBeatJob) {
        this.heartBeatJob = heartBeatJob;
    }

    public String getCheckHeartBeat() {
        return checkHeartBeat;
    }

    public void setCheckHeartBeat(String checkHeartBeat) {
        this.checkHeartBeat = checkHeartBeat;
    }

    public ApplicationContext getCtx() {
        return ctx;
    }

    public void setCtx(ApplicationContext ctx) {
        this.ctx = ctx;
    }

}
